package com.diligent.cloud.sfc.service.impl;

import com.alibaba.fastjson.JSON;
import com.canny.commons.model.Codes;
import com.canny.commons.model.ListResult;
import com.canny.commons.model.ObjectResult;
import com.canny.commons.page.IPage;
import com.canny.commons.page.Pagination;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.diligent.cloud.logistics.container.enums.BoxFillDocStatus;
import com.diligent.cloud.logistics.container.enums.BoxStatus;
import com.diligent.cloud.logistics.container.model.LcmBoxFillDetail;
import com.diligent.cloud.logistics.container.model.LcmBoxFillMaster;
import com.diligent.cloud.logistics.container.model.LcmBoxLedger;
import com.diligent.cloud.logistics.container.service.LcmBoxFillDetailFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxFillMasterFeignService;
import com.diligent.cloud.logistics.container.service.LcmBoxLedgerFeignService;
import com.diligent.cloud.primary.data.feign.service.imat.IMatImageFeignService;
import com.diligent.cloud.primary.data.feign.service.mdm.MdmBaseFeignService;
import com.diligent.cloud.primary.data.model.Result;
import com.diligent.cloud.primary.data.model.rt.RtMachine;
import com.diligent.cloud.primary.data.model.rt.RtWorkCenter;
import com.diligent.cloud.prodbatch.model.MfgProdBatch;
import com.diligent.cloud.prodbatch.service.MfgProdBatchFeignService;
import com.diligent.cloud.sfc.config.OauthConfig;
import com.diligent.cloud.sfc.config.ThreadPoolConfig;
import com.diligent.cloud.sfc.model.MatType;
import com.diligent.cloud.sfc.model.MfgConsume;
import com.diligent.cloud.sfc.model.OrderCompleteParams;
import com.diligent.cloud.sfc.model.OrderType;
import com.diligent.cloud.sfc.model.info.WoodenCaseInfo;
import com.diligent.cloud.sfc.model.info.inventory.MatInventoryInfo;
import com.diligent.cloud.sfc.model.params.inventory.PackingInventoryParams;
import com.diligent.cloud.sfc.model.result.LotsByMachCodeResult;
import com.diligent.cloud.sfc.model.result.PackDpsOrderResult;
import com.diligent.cloud.sfc.service.ISfcService;
import com.diligent.cloud.sfc.service.WorkshopScenePackingService;
import com.diligent.cloud.web.model.PageAndCondition;
import com.diligent.cloud.wip.core.feign.service.WipStorageLedgerFeignService;
import com.diligent.cloud.wip.panel.modle.WipStorageLedger;
import com.diligent.cloud.workorder.enums.WoLotsStatus;
import com.diligent.cloud.workorder.model.*;
import com.diligent.cloud.workorder.service.WoLotsFeignService;
import com.diligent.cloud.workorder.service.WoLotsWeightFeignService;
import com.diligent.cloud.workorder.service.WoOrderFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeDetailFeignService;
import com.diligent.cloud.workordermd.feign.service.MfgConsumeMasterFeignService;
import com.diligent.cloud.workordermd.model.MfgConsumeDetail;
import com.diligent.cloud.workordermd.model.MfgConsumeMaster;
import com.diligent.foundation.data.model.v2.Condition;
import com.google.common.base.Preconditions;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * WorkshopScenePackingServiceImpl class
 *
 * @author xf107278
 * @date 4/20/2020
 */
@Service
public class WorkshopScenePackingServiceImpl implements WorkshopScenePackingService {
    private ISfcService sfcService;
    private MfgConsumeMasterFeignService mfgConsumeMasterFeignService;
    private MfgConsumeDetailFeignService mfgConsumeDetailFeignService;
    private final WoOrderFeignService orderFeignService;
    private final WoLotsFeignService woLotsFeignService;
    private final OauthConfig.RequestParams requestParams;
    private final MfgProdBatchFeignService mfgProdBatchFeignService;
    private final IMatImageFeignService iMatImageFeignService;
    private final WoLotsWeightFeignService woLotsWeightFeignService;
    private final MdmBaseFeignService mdmBaseFeignService;
    private final LcmBoxLedgerFeignService lcmBoxLedgerFeignService;
    private final LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService;
    private final LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService;
    private final WipStorageLedgerFeignService wipStorageLedgerFeignService;

    private final ThreadPoolExecutor executor;

    public WorkshopScenePackingServiceImpl(ISfcService sfcService,
                                           MfgConsumeMasterFeignService mfgConsumeMasterFeignService,
                                           MfgConsumeDetailFeignService mfgConsumeDetailFeignService,
                                           WoOrderFeignService orderFeignService,
                                           WoLotsFeignService woLotsFeignService,
                                           OauthConfig.RequestParams requestParams, MfgProdBatchFeignService mfgProdBatchFeignService, IMatImageFeignService iMatImageFeignService, WoLotsWeightFeignService woLotsWeightFeignService, MdmBaseFeignService mdmBaseFeignService, LcmBoxLedgerFeignService lcmBoxLedgerFeignService, LcmBoxFillMasterFeignService lcmBoxFillMasterFeignService, LcmBoxFillDetailFeignService lcmBoxFillDetailFeignService, WipStorageLedgerFeignService wipStorageLedgerFeignService
    , ThreadPoolConfig threadPoolConfig) {
        this.sfcService = sfcService;
        this.mfgConsumeMasterFeignService = mfgConsumeMasterFeignService;
        this.mfgConsumeDetailFeignService = mfgConsumeDetailFeignService;
        this.orderFeignService = orderFeignService;
        this.woLotsFeignService = woLotsFeignService;
        this.requestParams = requestParams;
        this.mfgProdBatchFeignService = mfgProdBatchFeignService;
        this.iMatImageFeignService = iMatImageFeignService;
        this.woLotsWeightFeignService = woLotsWeightFeignService;
        this.mdmBaseFeignService = mdmBaseFeignService;
        this.lcmBoxLedgerFeignService = lcmBoxLedgerFeignService;
        this.lcmBoxFillMasterFeignService = lcmBoxFillMasterFeignService;
        this.lcmBoxFillDetailFeignService = lcmBoxFillDetailFeignService;
        this.wipStorageLedgerFeignService = wipStorageLedgerFeignService;
        this.executor=threadPoolConfig.threadPool();
    }

    @Override
    public ListResult getPackingList(String machCode, String matCode) {
        return sfcService.getMatList(machCode, OrderType.包装工单, matCode);
    }

    @Override
    public ObjectResult addPackingInventory(PackingInventoryParams packingInventoryParams, Principal principal) {
        MfgConsumeMaster consumeMaster ;
        MfgConsumeDetail consumeDetail ;
        for (MatInventoryInfo matInventoryInfo : packingInventoryParams.getPackingInventoryInfos()) {

            MfgConsume mfgConsume = sfcService.addMatInventoryInfo(matInventoryInfo
                    , sfcService.addMatConsumerInventory(packingInventoryParams, principal));

            consumeMaster = mfgConsume.getMfgConsumeMaster();
            consumeDetail = mfgConsume.getMfgConsumeDetail();

            mfgConsumeMasterFeignService.insert(consumeMaster);
            mfgConsumeDetailFeignService.insert(consumeDetail);
        }
        return new ObjectResult("");
    }

    @Override
    public ListResult getLotsByOrder(Long orderId) {
        ResponseEntity<Result> orderResult =
                orderFeignService.selectById(orderId);
        Preconditions.checkNotNull(orderResult.getBody(),"查询不到此工单");
        WoOrder order = (WoOrder) orderResult.getBody().getData();
        PageAndCondition pageAndCondition = new PageAndCondition();
        ArrayList<Condition> conditions = new ArrayList<>();

        conditions.add(new Condition("woOrder", "eq", order.getWoOrder(), null));
        conditions.add(new Condition("isCanceled", "eq", 0, null));

        pageAndCondition.setPagination(new Pagination(1L, 10000L));
        pageAndCondition.setConditions(conditions);
        ResponseEntity<Result<IPage<WoLots>>> page = woLotsFeignService.page(pageAndCondition);

        ListResult result;
        if (page.getBody()!=null){
             result = new ListResult(page.getBody().getData().getRecords());
            result.modifyCode(page.getBody().getCode(), page.getBody().getData().getTotal().toString());

        }else {
            result=new ListResult();
        }
        return result;
    }
    @Override
    public ObjectResult getLotsByMachCode(String machCode, Principal principal) {
        PageAndCondition pageAndCondition = new PageAndCondition();
        ArrayList<Condition> conditions = new ArrayList<>();
        //找最近修改的扎号
        conditions.add(new Condition("modifyTime","orderBy","DESC",null));
        conditions.add(new Condition("machCode","eq",machCode,null));
        conditions.add(new Condition("isCanceled", "eq", 0, null));
        conditions.add(new Condition("lotState", "eq", WoLotsStatus.USELOTS.getStatus(), null));
        conditions.add(new Condition("periodId", "eq", requestParams.getCompanyId(), null));
        conditions.add(new Condition("systemId", "eq", requestParams.getSystemId(), null));

        pageAndCondition.setConditions(conditions);
        pageAndCondition.setPagination(new Pagination(1L, 1L));

        ResponseEntity<Result<IPage<WoLots>>> page = woLotsFeignService.page(pageAndCondition);
        //获取工单
        if (page.getBody()!=null && page.getBody().getData().getRecords().size()!=0){
            WoLots woLots = page.getBody().getData().getRecords().stream().findFirst().get();

            //查询工单是否完工
            conditions = new ArrayList<>();
            WoOrderCompleteDetail orderCompleteByOrder = sfcService.getOrderCompleteByOrder(null, woLots.getWoOrder());
            if (orderCompleteByOrder!=null){ return new ObjectResult();}
            LotsByMachCodeResult lotsByMachCodeResult = new LotsByMachCodeResult();
            //未完工生产批号


            pageAndCondition.setConditions(Collections.singletonList(new Condition("woOrder","eq",woLots.getWoOrder(),null)));


            ResponseEntity<Result<IPage<WoOrder>>> orderPage = orderFeignService.page(pageAndCondition);
            Preconditions.checkNotNull(orderPage.getBody(),"查询不到此[%s]工单",woLots.getWoOrder());
            WoOrder order =
                    orderPage.getBody().getData().getRecords().stream().findFirst().get();

            ResponseEntity<Result<MfgProdBatch>> byBatchLast = mfgProdBatchFeignService.getByBatchLast(machCode,
                    requestParams.getSystemId());
            if (byBatchLast.getBody()!=null){
                MfgProdBatch mfgProdBatch = byBatchLast.getBody().getData();
                order.setProdBatch(mfgProdBatch.getProdBatch());
            }
            //查询扎号
            lotsByMachCodeResult.setOrder(order);

            conditions.add(new Condition("woOrder","eq",woLots.getWoOrder(),null));
            conditions.add(new Condition("isCanceled","eq",0,null));

            pageAndCondition.setPagination(new Pagination(1L,100L));
            pageAndCondition.setConditions(conditions);
            ResponseEntity<Result<IPage<WoLots>>> woLotsPage = woLotsFeignService.page(pageAndCondition);

            lotsByMachCodeResult.setLots(woLotsPage.getBody().getData().getRecords());

            return new ObjectResult(lotsByMachCodeResult);
        }

        return new ObjectResult();
    }

    @Override
    public ObjectResult getPackLotsFilter(String machCode, Principal principal) {
        List<WoodenCaseInfo> answer= new ArrayList<>();

        PageAndCondition pageAndCondition = new PageAndCondition();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(new Condition("location", "eq", machCode, null));
        conditions.add(new Condition("state", "in",
                BoxStatus.INSTALLING.getStatus() + "," + BoxStatus.INSTALLED.getStatus(), null));
        conditions.add(new Condition("modifyTime", "orderBy", "DESC", null));
        pageAndCondition.setPagination(new Pagination(1L, 1L));
        pageAndCondition.setConditions(conditions);
        ResponseEntity<Result> lcmBoxLedgeResult = lcmBoxLedgerFeignService.search(pageAndCondition);
        if (lcmBoxLedgeResult.getBody()!=null&&((IPage<LcmBoxLedger>) lcmBoxLedgeResult.getBody().getData()).getRecords().size()!=0) {
            IPage<LcmBoxLedger> page = (IPage<LcmBoxLedger>) lcmBoxLedgeResult.getBody().getData();
            LcmBoxLedger lcmBoxLedger = page.getRecords().stream().findFirst().get();
            //根据容器台账找到未结束的装单

            conditions = new ArrayList<>();
            conditions.add(new Condition("boxId", "eq", lcmBoxLedger.getId(), null));
            conditions.add(new Condition("isCanceled", "eq", 0, null));
            conditions.add(new Condition("isFinished", "eq", 0, null));
            conditions.add(new Condition("isClosed", "eq", 0, null));
            conditions.add(new Condition("docType", "eq", BoxFillDocStatus.INSERT.getStatus(), null));
            conditions.add(new Condition("modifyTime", "orderBy", "DESC", null));
            pageAndCondition.setConditions(conditions);
            pageAndCondition.setPagination(new Pagination(1L, 1L));
            ResponseEntity<Result<IPage<LcmBoxFillMaster>>> lcmBoxFillMasterPage =
                    lcmBoxFillMasterFeignService.page(pageAndCondition);
            //查询修改工单的装卸主表

            if (lcmBoxFillMasterPage.getBody()==null){    return new ObjectResult(answer);}
            for (LcmBoxFillMaster lcmBoxFillMaster : lcmBoxFillMasterPage.getBody().getData().getRecords()) {
                pageAndCondition.setConditions(Collections.singletonList(new Condition("masterId", "eq",
                        lcmBoxFillMaster.getId(), null)));
                ResponseEntity<Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailFeignPage =
                        lcmBoxFillDetailFeignService.page(pageAndCondition);

                if (lcmBoxFillDetailFeignPage.getBody()==null ) {
                    continue;
                }
                LcmBoxFillDetail lcmBoxFillDetail =
                        lcmBoxFillDetailFeignPage.getBody().getData().getRecords().stream().findFirst().get();

                WoOrderCompleteDetail orderCompleteByOrder =
                        sfcService.getOrderCompleteByOrder(Long.valueOf(lcmBoxFillDetail.getRefWoOrder()), null);

                //查询工单是否完工
                if (orderCompleteByOrder != null) {
                    pageAndCondition.setPagination(new Pagination(1L, 100000L));
                    pageAndCondition.setConditions(Collections.singletonList(new Condition("refWoOrder", "eq", lcmBoxFillDetail.getRefWoOrder(), null)));

                    ResponseEntity<Result>   orderResult =
                            orderFeignService.selectById(orderCompleteByOrder.getWoOrderId());
                    Preconditions.checkNotNull(orderResult.getBody(),"查询不到此工单");
                    ResponseEntity<Result<IPage<LcmBoxFillDetail>>> lcmBoxFillDetailPage =
                            lcmBoxFillDetailFeignService.page(pageAndCondition);
                    //查询到工单所有的装卸单
                if (lcmBoxFillDetailPage.getBody()==null){return new ObjectResult(answer);}
                    for (LcmBoxFillDetail boxFillDetail1 : lcmBoxFillDetailPage.getBody().getData().getRecords()) {
                        WoodenCaseInfo woodenCaseInfo = new WoodenCaseInfo();
                        woodenCaseInfo.setBoxId(boxFillDetail1.getRefBoxCode());
                        woodenCaseInfo.setBoxCode(Collections.singletonList(boxFillDetail1.getMatCode()));
                        woodenCaseInfo.setStandardDigit(((WoOrder)orderResult.getBody().getData()).getDefectDigit().longValue());
                        woodenCaseInfo.setActualDigit(boxFillDetail1.getDigit().intValue());
                        woodenCaseInfo.setTheoryWeight(boxFillDetail1.getTheoryWeight());
                        //查询台账
                        pageAndCondition.setPagination(new Pagination(1L, 1L));
                        pageAndCondition.setConditions(Collections.singletonList(new Condition("code", "eq",
                                boxFillDetail1.getRefBoxCode(), null)));
                        ResponseEntity<Result> search = lcmBoxLedgerFeignService.search(pageAndCondition);
                        if (search.getBody()!=null){
                            LcmBoxLedger boxLedger =
                                    ((IPage<LcmBoxLedger>) search.getBody().getData()).getRecords().stream().findFirst().get();

                            woodenCaseInfo.setWeight(boxLedger.getMatTotalWeight());
                        }

                        answer.add(woodenCaseInfo);
                    }
                }
            }
        }
        return new ObjectResult(answer);
    }

    @Override
    public ObjectResult getLastOrder(String machCode, Principal principal) {
        PageAndCondition pageAndCondition = new PageAndCondition();
        ArrayList<Condition> conditions = new ArrayList<>();

        conditions.add(new Condition("modifyTime","orderBy",null,null));
        conditions.add(new Condition("machCode","eq",machCode,null));
        conditions.add(new Condition("isCanceled", "eq", 0, null));
        conditions.add(new Condition("lotState", "eq", WoLotsStatus.USELOTS.getStatus(), null));
        conditions.add(new Condition("periodId", "eq", requestParams.getCompanyId(), null));
        conditions.add(new Condition("systemId", "eq", requestParams.getSystemId(), null));

        pageAndCondition.setConditions(conditions);
        pageAndCondition.setPagination(new Pagination(1L, 1L));
        ResponseEntity<Result<IPage<WoLots>>> page = woLotsFeignService.page(pageAndCondition);

        if (page.getBody()==null && page.getBody().getData()==null
                &&page.getBody().getData().getRecords().size()==0){return new ObjectResult();}

        WoLots woLots = page.getBody().getData().getRecords().stream().findFirst().get();

        pageAndCondition.setConditions(Collections.singletonList(new Condition("WoOrder","eq",woLots.getWoOrder(),null)));
        ResponseEntity<Result<IPage<WoOrder>>> orderPageResult = orderFeignService.page(pageAndCondition);

        WoOrder woOrder = orderPageResult.getBody().getData().getRecords().stream().findFirst().get();

            ResponseEntity<Result<MfgProdBatch>> byBatchLast = mfgProdBatchFeignService.getByBatchLast(machCode,
                    requestParams.getSystemId());
        if (byBatchLast.getBody()!=null){
            MfgProdBatch mfgProdBatch = byBatchLast.getBody().getData();
            woOrder.setProdBatch(mfgProdBatch.getProdBatch());
        }

        if (iMatImageFeignService.getMouldPic(woOrder.getMatCode()).getBody()!=null){
            woOrder.setImg(iMatImageFeignService.getMouldPic(woOrder.getMatCode()).getBody().getData().toString());
        }


        return new ObjectResult(woOrder);
    }



    @Override
    public ObjectResult cancelLots(Long lotsId, Principal principal) {
        ResponseEntity<Result> woLotsResult = woLotsFeignService.selectById(lotsId);
        if (woLotsResult.getBody()==null && woLotsResult.getBody().getCode()!=0){
            ObjectResult result = new ObjectResult();
            result.modifyCode(Codes.fail.getCode(),String.format("查询不到此[%s]扎",lotsId));
            return result;
        }
        WoLots lots = (WoLots) woLotsResult.getBody().getData();
        lots.setModifyCode(principal.getName());
        lots.setModifyName(principal.getName());
        lots.setModifyTime(new Date());
        lots.setIsCanceled(1);
        ResponseEntity<Result> update = woLotsFeignService.update(lots);
        if (update.getBody()!=null && update.getBody().getData()!=null){
            return new ObjectResult(update.getBody().getData());
        }
        ObjectResult result = new ObjectResult();
        result.modifyCode(Codes.fail.getCode(),"取消失败");
        return result;
    }

    @Override
    public ObjectResult addLotsWeight(WoLotsWeight lotsWeight, Principal principal) {

        lotsWeight.setEnterCode(principal.getName());
        lotsWeight.setEnterName(principal.getName());
        lotsWeight.setEnterTime(new Date());
        lotsWeight.setSystemId(requestParams.getSystemId());
        lotsWeight.setPeriodId(sfcService.getPeriodId());

        ResponseEntity<Result> insert = woLotsWeightFeignService.insert(lotsWeight);
        if (insert.getBody()!=null&& insert.getBody().getData()!=null){
            return new ObjectResult(insert.getBody().getData());
        }
        ObjectResult result = new ObjectResult();
        result.modifyCode(Codes.fail.getCode(),"新增过磅失败");
        return  result;
    }

    @Override
    @Transactional
    @LcnTransaction
    public ObjectResult addLots(OrderCompleteParams orderCompleteParams, Principal principal) {
        ResponseEntity<Result> orderResult = orderFeignService.selectById(orderCompleteParams.getOrderId());
        WoOrder order = (WoOrder) orderResult.getBody().getData();
        WoLots woLots  = new WoLots();


        BeanUtils.copyProperties(order,woLots);
        woLots.setDigit(orderCompleteParams.getDigit());
        woLots.setMachCode(order.getMachineCode());
        woLots.setOrderDigit(order.getDeliveryDigit());
        woLots.setLength(new BigDecimal(order.getCv4()));
        woLots.setLotState(WoLotsStatus.USELOTS.getStatus());
        woLots.setEnterCode(principal.getName());
        woLots.setEnterName(principal.getName());
        woLots.setCwocCode(order.getWorkCenterCode());
        woLots.setEnterTime(new Date());
        woLots.setModifyTime(new Date());
        woLots.setModifyName(principal.getName());
        woLots.setModifyCode(principal.getName());
        woLots.setPackWeight(orderCompleteParams.getWeight());
        woLots.setStdPackDigit(order.getStandardDigit());
        woLots.setSystemId(requestParams.getSystemId());
        woLots.setCompanyId(requestParams.getCompanyId());
        woLots.setPeriodId(sfcService.getPeriodId());

        ResponseEntity<Result> insert = woLotsFeignService.insert(woLots);
        if (insert.getBody()!=null && insert.getBody().getData()!=null){
            woLots = (WoLots) insert.getBody().getData();

            WoLotsWeight woLotsWeight = new WoLotsWeight();
            woLotsWeight.setLotId(woLots.getId());
            woLotsWeight.setLotCode(woLots.getLotCode());
            woLotsWeight.setWeight1(orderCompleteParams.getWeight());
            this.addLotsWeight(woLotsWeight,principal);

            return new ObjectResult(insert.getBody().getData());
        }
        ObjectResult result = new ObjectResult();
        result.modifyCode(Codes.fail.getCode(),"新增失败");
        return result;
    }

    @Override
    public ListResult getPackDpsFilter(DpsListParams dpsListParams, ResponseEntity<ListResult> dpsList) {

        List<PackDpsOrderResult> answer = new ArrayList<>();
        if (dpsList.getBody()!=null && dpsList.getBody().getData()!=null){
            RtMachine machineByMachCode = sfcService.getMachineByMachCode(dpsListParams.getMachineCode());
            Preconditions.checkNotNull(machineByMachCode,"查询不到此机台");
            RtWorkCenter workCenterByCwocCode = sfcService.getWorkCenterByCwocCode(machineByMachCode.getCwocCode());
            Preconditions.checkNotNull(workCenterByCwocCode,"查询不到此车间");

            String matType = sfcService.getMatType(MatType.包装物);
            PackDpsOrderResult packDpsOrderResult;



            final CountDownLatch countDownLatch=new CountDownLatch(dpsList.getBody().getData().size());
            for (LinkedHashMap orderMap:(List<LinkedHashMap>)dpsList.getBody().getData()) {
                WoOrder order = JSON.parseObject(JSON.toJSONString(orderMap), WoOrder.class);
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            test(order,answer,workCenterByCwocCode,matType);
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }finally {
                            countDownLatch.countDown();
//                                         System.out.println("线程池中线程数目：" + executor.getPoolSize() +
//                                                 "，队列中等待执行的任务数目：" + executor.getQueue().size() +
//                                                 "，已执行完毕的任务数目：" + executor.getCompletedTaskCount());
                        }
                    }
                });
//                packDpsOrderResult = new PackDpsOrderResult();
//                WoOrder order = JSON.parseObject(JSON.toJSONString(orderMap), WoOrder.class);
//                ObjectResult<WipStorageLedger> ledger =
//                        wipStorageLedgerFeignService.getLedger(workCenterByCwocCode.getStorageCode(), order.getCv8(),
//                                null, order.getSystemId());
//                if (ledger.getData()!=null){
//
//                    List<WoOrderMatNeed> matNeedByOrderIdAndMatType =
//                            sfcService.getMatNeedByOrderIdAndMatType(order.getId(), matType);
//                    Preconditions.checkNotNull(matNeedByOrderIdAndMatType,"查询不到此工单的包装物料需求信息");
//
//                    if (ledger.getData().getDigit().intValue() != 0){
//                        packDpsOrderResult.setPackStock((matNeedByOrderIdAndMatType.stream().findFirst().get().getDigit().intValue() / ledger.getData().getDigit().intValue())*100);
//                    }else {
//                        packDpsOrderResult.setPackStock(0);
//                    }
//                    BeanUtils.copyProperties(order,packDpsOrderResult);
//                    Map<String, Integer> projectMatchingRate = sfcService.getProjectMatchingRate(order.getOrderType()
//                            , order.getProjectName(), order.getMachineCode());
//
//                    packDpsOrderResult.setProjectMatDigit(projectMatchingRate.get("matCodeNb"));
//                    packDpsOrderResult.setProjectStock(projectMatchingRate.get("total"));
//                    answer.add(packDpsOrderResult);
//                }

            }
            try {
                //等待子线程全部结束
                countDownLatch.await();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ListResult result = new ListResult(answer);
        result.modifyCode(Codes.success.getCode(),dpsList.getBody().getMsg());
        return result;
    }
    private void test (WoOrder order,   List<PackDpsOrderResult> answer, RtWorkCenter workCenterByCwocCode,String matType){
        PackDpsOrderResult  packDpsOrderResult = new PackDpsOrderResult();
     //   WoOrder order = JSON.parseObject(JSON.toJSONString(orderMap), WoOrder.class);
        ObjectResult<WipStorageLedger> ledger =
                wipStorageLedgerFeignService.getLedger(workCenterByCwocCode.getStorageCode(), order.getCv8(),
                        null, order.getSystemId());
        if (ledger.getData()!=null){

            List<WoOrderMatNeed> matNeedByOrderIdAndMatType =
                    sfcService.getMatNeedByOrderIdAndMatType(order.getId(), matType);
            Preconditions.checkNotNull(matNeedByOrderIdAndMatType,"查询不到此工单的包装物料需求信息");

            if (ledger.getData().getDigit().intValue() != 0){
                packDpsOrderResult.setPackStock((matNeedByOrderIdAndMatType.stream().findFirst().get().getDigit().intValue() / ledger.getData().getDigit().intValue())*100);
            }else {
                packDpsOrderResult.setPackStock(0);
            }
            BeanUtils.copyProperties(order,packDpsOrderResult);
            Map<String, Integer> projectMatchingRate = sfcService.getProjectMatchingRate(order.getOrderType()
                    , order.getProjectName(), order.getMachineCode());

            packDpsOrderResult.setProjectMatDigit(projectMatchingRate.get("matCodeNb"));
            packDpsOrderResult.setProjectStock(projectMatchingRate.get("total"));
            answer.add(packDpsOrderResult);
        }

    }




}
